package ua.yandex.books;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Book {

    enum Topic {

        COMPUTING, FANTASY, CLASSICS, FICTION
    }

    private String title;
    private List<String> authors;
    private int yearOfPublising;
    private Topic topic;

    public Book(String str, int year, Topic top, List<String> aut) {
        this.title = str;
        this.authors = aut;
        this.yearOfPublising = year;
        this.topic = top;
    }

    public String getTitle() {
        return this.title;
    }

    public static Map<String, List<Book>>
            groupBooksByAuthors(List<Book> books) {
        Map<String, List<Book>> format = new HashMap<String, List<Book>>();
        for (Book book : books) {
            for (String str : book.authors) {
                if (format.containsKey(str)) {
                    format.get(str).add(book);
                } else {
                    List<Book> lb = new LinkedList<Book>();
                    lb.add(book);
                    format.put(str, lb);
                }
            }
        }

        return format;
    }

    public static Map<Integer, Set<String>>
            findTopAuthorsPerEachYear(List<Book> books) {
        Map<Integer, Set<String>> format = new HashMap<Integer, Set<String>>();
        Map<Integer, List<Book>> year = new HashMap<Integer, List<Book>>();
        Set<Integer> yearList = new HashSet<Integer>();
        for (Book book : books) {
            if (year.containsKey(book.yearOfPublising)) {
                year.get(book.yearOfPublising).add(book);
            } else {
                List<Book> lb = new LinkedList<Book>();
                lb.add(book);
                year.put(book.yearOfPublising, lb);
            }
            yearList.add(book.yearOfPublising);
        }

        for (int i : yearList) {
            List<Book> lb = year.get(i);
            Map<String, List<Book>> slb = groupBooksByAuthors(lb);
            //получил список книг авторов за конкретный год
            int max = 0;
            Set<String> se = new HashSet<String>();
            for (Map.Entry<String, List<Book>> entry : slb.entrySet()) {
                //имем автора и список книг за год
                if (entry.getValue().size() > max) {
                    max = entry.getValue().size();
                    se.clear();
                    se.add(entry.getKey());
                } else if (entry.getValue().size() == max) {
                    se.add(entry.getKey());
                }
            }
            format.put(i, se);
        }
        return format;
    }

    //Set<Tuple<Topic,List<String>>>
    public static Set<Tuple<Topic, List<String>>>
            findTopicsWithTheMostNumberOfBooks(List<Book> books) {
        Set<Tuple<Topic, List<String>>> titles = new HashSet<Tuple<Topic, List<String>>>();
        for (Book book : books) {
            for (Tuple<Topic, List<String>> t : titles) {
                if (t.getValue() == book.topic) {
                    t.addValue(book.title);
                    ;
                }
                ;
            }
        }
        return titles;
    }

}
